package com.kzf.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.kzf.mapper.ClassMapper;
import com.kzf.pojo.*;
import com.kzf.service.ClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Service
public class ClassServiceImpl implements ClassService {
    @Autowired
    private ClassMapper classMapper;
    @Override
    public ClassResult<ClazzDTO> getList(ClassQueryParam classQueryParam) {
        //1. 设置分页参数(PageHelper)
        PageHelper.startPage(classQueryParam.getPage(), classQueryParam.getPageSize());

        //2. 执行查询
        List<ClazzDTO> ClassList = classMapper.getList(classQueryParam);

        //3. 解析查询结果, 并封装
        Page<ClazzDTO> p = (Page<ClazzDTO>) ClassList;
        return new ClassResult<ClazzDTO>(p.getTotal(), p.getResult());

    }

    @Override
    public void delete(Integer id) {
        classMapper.delete(id);
    }

    @Override
    public void insert(Clazz clazz){
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        classMapper.insert(clazz);
    }

    @Override
    public ClassById getById(Integer id) {
        return classMapper.getById(id);
    }

    @Override
    public void update(Clazz clazz) {
        clazz.setUpdateTime(LocalDateTime.now());
        classMapper.update(clazz);
    }

    @Override
    public List<ClassById> getAll(){
        return classMapper.getAll();
    }
}
